package com.itheima.dubbo.mongodb.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.commons.model.mongo.Voice;
import com.itheima.dubbo.interfaces.TaohuaApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service
public class TaohuaApiImpl implements TaohuaApi {

    @Autowired
    private MongoTemplate mongoTemplate;


    /**
     * 根据用户id查询语音
     *
     * @param userId
     * @return
     */
    @Override
    public List<Voice> queryVoiceById(Long userId) {
        List<Voice> voices = mongoTemplate.find(Query.query(Criteria.where("userId").is(userId)), Voice.class);
        return voices;
    }

    /**
     * 查询语音集合
     *
     * @return
     */
    @Override
    public List<Voice> queryVoices() {
        List<Voice> voices = mongoTemplate.findAll(Voice.class);
        return voices;
    }


    @Override
    public String saveVoice(Voice voice) {
        mongoTemplate.save(voice);
        return voice.getUserId().toString();
    }
}
